﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Xml;
using WordTemplate1.Methodes;

namespace WordTemplate1.WPF
{
    /// <summary>
    /// Logique d'interaction pour WorkFlow.xaml
    /// </summary>
    public partial class WorkFlow : UserControl
    {
        public WorkFlow()
        {
            InitializeComponent();
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (Globals.ThisDocument.taskPanel.trvWI.SelectedNode != null)
            {
                if (Globals.ThisDocument.taskPanel.trvWI.SelectedNode.Tag is WorkItem)
                {
                    WorkItem wi = Globals.ThisDocument.taskPanel.trvWI.SelectedNode.Tag as WorkItem;
                    List<WordTemplate1.Methodes.MethodesExtention.Transition> lt = wi.Type.GetTransistions();
                    Dictionary<string, Step> lb = new Dictionary<string, Step>();

                    double to = 60;

                    foreach (WordTemplate1.Methodes.MethodesExtention.Transition t in lt)
                    {
                        Step FromStep = new Step();
                        FromStep.Text = t.From;
                        FromStep.ToolTip = t.From;

                        if (String.Compare(t.From, wi.State, true) == 0)
                        {
                            FromStep.StateActiv = true;
                            FromStep.assignTo.Content = wi.Fields["System.ChangedBy"].Value.ToString();
                            FromStep.Date.Content = wi.Fields["System.ChangedDate"].Value.ToString();
                        }

                        Step ToStep = new Step();
                        ToStep.Text = t.To;
                        ToStep.ToolTip = t.To;

                        GetHytorique(wi, FromStep);
                        GetHytorique(wi, ToStep);

                        if (String.Compare(t.To, wi.State, true) == 0)
                        {
                            ToStep.StateActiv = true;
                            ToStep.assignTo.Content = wi.Fields["System.ChangedBy"].Value.ToString();
                            ToStep.Date.Content = wi.Fields["System.ChangedDate"].Value.ToString();
                        }

                        if (!lb.ContainsKey(t.From) && t.From != "")
                        {
                            lb.Add(t.From, FromStep);

                            TranslateTransform translateTransform = new TranslateTransform(to, 100);
                            FromStep.RenderTransform = translateTransform;

                            designer.Children.Add(FromStep);
                        }
                        else if (t.From != "")
                        {
                            FromStep = lb[t.From];
                        }

                        if (!lb.ContainsKey(t.To) && t.From != "")
                        {
                            lb.Add(t.To, ToStep);

                            TranslateTransform translateTransform = new TranslateTransform(to + 350, 100);
                            ToStep.RenderTransform = translateTransform;

                            designer.Children.Add(ToStep);
                        }

                        else if (t.From != "")
                        {
                            ToStep = lb[t.To];
                        }

                        if (t.From != "")
                        {
                            to = to + 350;
                        }
                        else if (t.From == "")
                        {
                            Start st = new Start();
                            st.ToolTip = "Start";

                            TranslateTransform translateTransform = new TranslateTransform(0, 115);
                            st.RenderTransform = translateTransform;

                            designer.Children.Add(st);
                        }
                    }
                }
            }
        }

        private bool isMouseCaptured;
        private Point clickPosition;

        //private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    ((Step)sender).CaptureMouse();
        //    clickPosition = e.GetPosition((Step)sender);
        //    isMouseCaptured = true;
        //}

        //private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        //{
        //    ((Step)sender).ReleaseMouseCapture();
        //    isMouseCaptured = false;
        //}

        //private void Border_MouseMove(object sender, MouseEventArgs e)
        //{
        //    if (isMouseCaptured)
        //    {
        //        Point newPosition = e.GetPosition(designer);
        //        Step element = (Step)sender;
        //        ((TranslateTransform)element.RenderTransform).X = newPosition.X - element.RenderTransformOrigin.X - clickPosition.X;
        //        ((TranslateTransform)element.RenderTransform).Y = newPosition.Y - element.RenderTransformOrigin.Y - clickPosition.Y;
        //    }
        //}

        private void GetHytorique(WorkItem wi,Step step)
        {
            foreach (Revision rev in wi.Revisions)
            {
                foreach (Field f in rev.Fields)
                {
                    if (f.ReferenceName == "System.State")
                    {
                        if (String.Compare(f.Value.ToString(), step.Text) == 0)
                        {
                            step.StatePassed = true;
                            step.assignTo.Content = rev.Fields["System.ChangedBy"].Value.ToString();
                            step.Date.Content = rev.Fields["System.ChangedDate"].Value.ToString();
                        }
                    }
                }
            }

        }
    }


}
